package com.qf.controller.popularize;

import com.alibaba.druid.util.StringUtils;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCoupon;
import com.qf.service.DtsCouponService;
import com.qf.service.DtsCouponUserService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller.popularize
 * @Description:
 * @Date 2022/10/19 22:06
 */
@RestController
@RequestMapping("/admin/coupon")
public class AdminCouponController {

    @Autowired
    DtsCouponService couponService;

    @Autowired
    DtsCouponUserService couponUserService;

//    /list?page=1&limit=20&name=%E6%A0%87%E9%A2%98&type=1&status=1&sort=add_time&order=desc
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page ,
                       @RequestParam(value = "limit",required = false,defaultValue = "20") Integer limit,
                       @RequestParam(value = "sort",required = false,defaultValue = "add_time") String sort,
                       @RequestParam(value = "order",required = false,defaultValue = "desc") String order,
                       String name,Integer type,Integer status){

        Map<String,Object> data = couponService.selectByPage(page,limit,sort,order,name,type,status);
        return ResponseUtil.ok(data);

    }

    @GetMapping("/read")
    public Object read(Integer id){
        DtsCoupon coupon = couponService.selectById(id);
        return ResponseUtil.ok(coupon);
    }

//    /listuser?page=1&limit=20&couponId=12&userId=12&status=1&sort=add_time&order=desc
    @GetMapping("/listuser")
    public Object listuser(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page ,
                           @RequestParam(value = "limit",required = false,defaultValue = "20") Integer limit,
                           @RequestParam(value = "sort",required = false,defaultValue = "add_time") String sort,
                           @RequestParam(value = "order",required = false,defaultValue = "desc") String order,
                           Integer couponId,Integer status){
        Map<String,Object> data = couponUserService.selectlistuserByPage(page,limit,sort,order,couponId,status);

        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object create(@RequestBody DtsCoupon coupon){
        Object result = checkDtsCoupon(coupon);
        if (result!=null){
            return result;
        }
        coupon.setAddTime(new Date());
        coupon.setDeleted(false);
        coupon.setUpdateTime(new Date());
//        System.out.println(coupon.toString());
        couponService.insert(coupon);

        return ResponseUtil.ok(coupon);
    }

    private Object checkDtsCoupon(DtsCoupon coupon) {
        if (StringUtils.isEmpty(coupon.getName()) || StringUtils.isEmpty(coupon.getDesc())|| StringUtils.isEmpty(coupon.getTag())){
            return ResponseUtil.fail(-1,"优惠价名称、介绍和标签不能为空");
        }
        if (coupon.getMin()==null || coupon.getTotal()==null || coupon.getLimit()== null || coupon.getDiscount()==null){
            return ResponseUtil.fail(-1,"优惠价最低消费、满减金额、每人限领和优惠卷数量不能为空");
        }

        return null;
    }


    @PostMapping("/update")
    public Object update(@RequestBody DtsCoupon coupon){
        coupon.setUpdateTime(new Date());
        couponService.update(coupon);
        return ResponseUtil.ok(coupon);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCoupon coupon){
        couponService.delete(coupon);
        return ResponseUtil.ok();
    }
}
